package com.nsgf.library.ui.page.home;

import android.app.Dialog;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.ClockInBean;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.data.bean.WorkOrderDetailBean;
import com.nsgf.library.data.response.DataResult;
import com.nsgf.library.databinding.ActivityNsgfCheckWorkAttendanceDetailsBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.adapter.ClockInfoListAdapter;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.utils.Kits;
import com.nsgf.library.utils.ToastUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class CheckWorkAttendanceDetailsActivity extends BaseActivity {
    private CheckWorkAttendanceState mStates;


    private Dialog dialog;

    private ActivityNsgfCheckWorkAttendanceDetailsBinding mBinding;

    private ClockInfoListAdapter mAdapter;

    AccountRequester mAccountRequester;
    Calendar calendar;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(CheckWorkAttendanceState.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);

    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new ClockInfoListAdapter(this);
        return new DataBindingConfig(R.layout.activity_nsgf_check_work_attendance_details, BR.vm, mStates)
                .addBindingParam(BR.adapter, mAdapter)
                .addBindingParam(BR.vmInfo, mStates.dataBeanState)
                .addBindingParam(BR.click, new ClickProxy());
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getLifecycle().addObserver(mAccountRequester);
        mBinding = (ActivityNsgfCheckWorkAttendanceDetailsBinding) getBinding();
        mBinding.titlebar.titleBarRoot.setText("");

        mStates.titleBar.set("考勤汇总");
        mAdapter.notifyDataSetChanged();
        mBinding.recyclerView.setNestedScrollingEnabled(false);

        calendar = Calendar.getInstance();
        mBinding.tvNsgfCheckInWorkAttendanceMonth.setText(Kits.Date.getYearMonth(calendar.getTimeInMillis()));


        mAccountRequester.getClockInResult().observe(this, new Observer<DataResult<ClockInBean>>() {
            @Override
            public void onChanged(DataResult<ClockInBean> clockInBeanDataResult) {

                if (clockInBeanDataResult.getResponseStatus().isSuccess()) {
                    if (clockInBeanDataResult.getResponseStatus().getResponseCode() == 200) {
                        if (null == clockInBeanDataResult.getResult().getData())
                            return;
                        List<ClockInBean.DataBean> list = new ArrayList<>();


                        for (ClockInBean.DataBean.LateAndEarlyListBean bean:clockInBeanDataResult.getResult().getData().getAttendanceList()) {
                            bean.setContent(bean.getClockDate());
                            bean.setClockDate(bean.getClockInTime() + "\n" + bean.getClockOutTime());
                        }
                        list.add(new ClockInBean.DataBean("出勤", clockInBeanDataResult.getResult().getData().getAttendanceList()));
                        list.add(new ClockInBean.DataBean("迟到/早退", clockInBeanDataResult.getResult().getData().getLateAndEarlyList()));
                        list.add(new ClockInBean.DataBean("缺卡", clockInBeanDataResult.getResult().getData().getUnClockList()));
                        list.add(new ClockInBean.DataBean("旷工", clockInBeanDataResult.getResult().getData().getAbsentList()));
                        mStates.list.set(list);
                        mStates.dataBeanState.set(clockInBeanDataResult.getResult().getData());
                    } else {

                        if (401 == clockInBeanDataResult.getResult().getCode()) {
                            UserInfo.getInstance().clearUserInfo();
                            finish();
                        }
                        showToast(clockInBeanDataResult.getResult().getMsg());
                    }
                } else {
                    ToastUtils.showLongToast("请稍后再试...");
                }

            }
        });

        mAccountRequester.punchSummary(UserInfo.getInstance().getProjectCode(), Kits.Date.getYearMonth(calendar.getTimeInMillis()));
        mStates.yearMonth.set(Kits.Date.getYearMonth(calendar.getTimeInMillis()) + "考勤汇总");
    }

    public static class CheckWorkAttendanceState extends StateHolder {

        public final State<List<ClockInBean.DataBean>> list = new State<>(new ArrayList<>());
        public final State<ClockInBean.DataBean> dataBeanState = new State<>(new ClockInBean.DataBean());
        public final State<Boolean> loadingVisible = new State<>(false);
        public final State<String> yearMonth = new State<>("");

    }


    public class ClickProxy {

        public void pMonth() {
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
            mBinding.tvNsgfCheckInWorkAttendanceMonth.setText(Kits.Date.getYearMonth(calendar.getTimeInMillis()));
            mAccountRequester.punchSummary(UserInfo.getInstance().getProjectCode(), mBinding.tvNsgfCheckInWorkAttendanceMonth.getText().toString());

            mStates.yearMonth.set(Kits.Date.getYearMonth(calendar.getTimeInMillis()) + "考勤汇总");

        }

        public void nMonth() {
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
            mBinding.tvNsgfCheckInWorkAttendanceMonth.setText(Kits.Date.getYearMonth(calendar.getTimeInMillis()));

            mAccountRequester.punchSummary(UserInfo.getInstance().getProjectCode(), mBinding.tvNsgfCheckInWorkAttendanceMonth.getText().toString());

            mStates.yearMonth.set(Kits.Date.getYearMonth(calendar.getTimeInMillis()) + "考勤汇总");
        }
    }


}
